﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Admin.Model
<script>
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            return false;
        }
    });
    var myTree;
    function loadTree() {
        myTree = dhtmlXTreeFromHTML("treeboxbox_tree"); // 加载检索树
        myTree.attachEvent("onClick", function (id) {
            selectSingle(id); //选择事件
        });
    }
    function addNode(idstr) {
        var codegrde = idstr;
        var clscode = $('#NavCode').val();
        myTree.deleteChildItems(codegrde);//加载之前先删除他的页子结点
        var url = timestamp("@Html.Raw(Url.Action("GetCateCodeChilds", "Admin_Topic"))?codestr=" + codegrde + "&clscode=" + clscode);//发送AJAX的URL
        $.getJSON(
          url,
          function (data) {
              $.each(data, function (index, entry) {//ajax回调后,循环.
                  var nodeid = entry['SySCode'] + "," + entry['Grade'] + "," + entry['SYSID'];
                  myTree.insertNewChild(codegrde, nodeid, entry['NodeName'], '', '', '', '', '', '');//使用这个函数.
                  if (entry['CHILDFLAG'] == 1) {
                      addNode(nodeid);
                  }
              });

          }
         );

    }
    function closeCate() {
        $(".talkDivBg").hide();
        $(".form-h-8em").hide();
    }
    function showCate() {
        $(".talkDivBg").show();
        $(".form-h-8em").show();
    }
    $(document).ready(function () {
        loadTree();

    });
    function selectSingle(codegrade) {
        $("#CodeGrade").val(codegrade);
    }
    function addnextcate() {
        if (isSelectnode()) {
            var codestr = $("#CodeGrade").val();
            var arry = codestr.split(',');
            if (arry[1] == 2) {
                messageAlert("您选择的节点已经是二级分类");
                return;
            }

            $("#catename").val("");
            $("#Flage").val(1);//标识增加下级节点
            $(".talkDiv-title span").html("新建二级分类");
            showCate();
        }
    }
    function addfirstcate() {
        $("#catename").val("");
        $("#Flage").val(0);//标识增加一级节点
        $(".talkDiv-title span").html("新建一级分类");
        showCate();

    }
    function savecate() {
        var navCode = $('#NavCode').val();
        var name = $('#catename').val();
        if ($.trim(name) == "") {
            messageAlert("请输入分类名称！");
            return;
        }
        else {
            if (!regtext(name)) {
                messageAlert("请输入分类名称(不允许输入特殊字符)！");
                return;
            }
        }
        var flage = $("#Flage").val();
        var sysid = 0;
        var grade = 0;
        var parentcode = "000";
        var codestr = $("#CodeGrade").val();
        if (flage != "0") {
            var arry = codestr.split(',');
            if (flage == 2) {  //更新时sysid赋值，新增时sysid为0
                sysid = arry[2];
            }
            parentcode = arry[0];
            grade = arry[1];
        }
        var data = { "ClsCode": navCode, "sysid": sysid, "NodeName": name, "parentcode": parentcode, "grade": grade};
        ;
        var delurl = "@Html.Raw(Url.Action("SaveNavCate", "Admin_Topic"))";
        dealdata(delurl, data);
        if (flage == 1) {
            addNode(codestr);//新增下级节点后加载节点下数据
        }
        else if (flage == 2) {  //更新成功后，刷新节点数据
            //设置节点名称
            myTree.setItemText(codestr, name, name);
        }
        else if (flage == 0) { //一级节点增加完成后，刷新tree
            ajaxLoad('initload', "@Html.Raw(Url.Action("CategorySet", "Admin_Topic"))?name=" + escape($('#NavName').val()) + "&code=" + navCode)
        }
    closeCate();
}
function isSelectnode() {
    var parentcode = $("#CodeGrade").val();
    if (parentcode == 0) {
        messageAlert("请选择节点！");
        return false;
    }
    else {
        return true;
    }
}
function updatecate() {
    if (isSelectnode()) {
        $("#Flage").val(2);//标识更新
        $(".talkDiv-title span").html("更新分类");
        var nodeid = $("#CodeGrade").val();
        var name = myTree.getItemText(nodeid);
        $("#catename").val(name);
        showCate();
    }
}
function deleteCate() {
    if (isSelectnode()) {
        confirmDialog('确定要删除吗?', function () {
            var str = $("#CodeGrade").val();
            var delurl = "@Html.Raw(Url.Action("DelNavCLs", "Admin_Topic"))";
            var data = { "syscodestr": str.split(',')[0], "clsCode": $("#NavCode").val() };
            dealdata(delurl, data);
            closeCate();
            myTree.deleteItem(str, true);
            $("#CodeGrade").val(0);//清空选择的节点
            myTree.clearSelection();
        })
    }
}
//调用controll
function dealdata(urlstr, datastr) {
    $.ajax({
        type: "POST",
        url: urlstr,
        data: datastr,
        success: function (m) {
            if (m == "True") {
                $("#CodeGrade").val(0);//清空选择的节点
                myTree.clearSelection();
                messageAlert("操作成功！");
            }
            else {
                messageAlert("操作失败！");
            }
            enablesave();
        },
        error: function (e)
        { enablesave(); }
    })
}
function disablesave() {
    $(".del").attr("disabled", "disabled");
}
function enablesave() {
    $(".del").removeAttr("disabled", "disabled");
}
function managercate()
{
    if (isSelectnode()) {
        var str = $("#CodeGrade").val();
        var name = myTree.getItemText(str);
        ajaxLoad('initload', "@Html.Raw(Url.Action("CategoryDataList", "Admin_Topic"))?name=" + escape(name) + "&code=" + str.split(',')[0] + "&navName=" + escape($("#NavName").val()) + "&navCode=" + $("#NavCode").val())
    }
}
    function additem()
    {
        if (isSelectnode()) {
            var str = $("#CodeGrade").val();
            var name = myTree.getItemText(str);
            ;
            ajaxLoad('initload', "@Html.Raw(Url.Action("ClsNodeQueryEdit", "Admin_Topic"))?name=" + escape(name) + "&code=" + str.split(',')[0] + "&navcode=" + $("#NavCode").val() + "&navname=" + escape($("#NavName").val()))
    }
    }
</script>
<style>
    span.selectedTreeRow {
        background-color: #e6e6e6 !important;
        width: 250px !important;
    }
    div{ cursor:default }
</style>
<div class="wrapR-c" style="margin-top: 10px">
    <h3 class='box-h'>分类导航管理—建立分类 </h3>
    <h3>专题导航： @ViewBag.NavName</h3>
    <input hidden="hidden" value="@ViewBag.NavCode" id="NavCode" type="hidden" />
    <input hidden="hidden" value="@ViewBag.NavName" id="NavName" type="hidden" />
    <input hidden="hidden" value="0" id="CodeGrade" type="hidden" />
    <input hidden="hidden" value="0" id="Flage" type="hidden" />
    <div class='posr' style="margin-bottom: 20px; padding-top: 20px">
        <button class="btn btn-m btn-submit btn-defa-f" onclick="addfirstcate()">新建一级分类</button>
        <button class="btn btn-m btn-submit btn-defa-f" onclick="addnextcate()">新建二级分类</button>
        <button class="btn btn-m btn-submit btn-defa-f" onclick="updatecate()">更新分类</button>
        <button class="btn btn-m btn-submit btn-defa-f" onclick="managercate()">配置分类条件</button>
@*        <button class="btn btn-m btn-submit btn-defa-f" onclick="additem()">添加记录</button>*@
        <button class="btn btn-m btn-submit btn-defa-f del" onclick="deleteCate()">删除</button>
        <button  class="btn btn-m btn-prim-f" onclick="ajaxLoad('initload', '@Html.Raw(Url.Action("CategoryNavManager", "Admin_Topic"))');">返回</button>
    </div>
    <div id="treeboxbox_tree" enabletreeimages="1" enableautotooltips="1" enablemultilineitems="1"
        setimagepath="~/Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/">
        <ul>
            @{ 
                if (ViewBag.CateList != null)
                {

                    int preFlag = 0;
                    foreach (SYS_CMS_TOPIC_CLS_NODE item in ViewBag.CateList)
                    {
                        if (item.Grade < preFlag)
                        {
                            for (int i = 0; i < preFlag - item.Grade; i++)
                            {
                @Html.Raw("</ul></li>");
                            }
                        }
                        preFlag = item.Grade;
                        var idstr = item.SySCode + "," + item.Grade + "," + item.SYSID;
                @Html.Raw("<li id=" + idstr + ">" + item.NodeName);
                                                                  if (1 == item.ChildFlag)
                                                                  {
                @Html.Raw("<ul>");
                                                                  }
                                                                  else
                                                                  {
    
                @Html.Raw("</li>");
                                                                  }
                    }
                    for (int i = 0; i < preFlag - 1; i++)
                    {
                @Html.Raw("</ul></li>");
                    }
                }
              
            }
        </ul>
        <ul>
        </ul>
    </div>

</div>

<div class="talkDivBg" style="display: none;">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkW-550 form-h form-h-8em" style="display: none">
        <div class="talkDiv-title">
            <span>添加分类</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeCate()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">分类名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" class="f-text span4" id="catename" maxlength="20">
                    <span class="f-label-re">*</span>
                </div>
            </div>


        </div>
        <div class="talkDiv-btn"><a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="savecate()">保存</a> </div>
    </div>
</div>

